'use strict';

app.controller('admin_detail_Ctrl', ['$scope','$http','loginService','$routeParams','adminService', function($scope,$http,loginService,$routeParams,adminService){
	
 //=====================paging groups of user======================
    $scope.entryLimit_group = 5
    $scope.offset_group = 0;
    $scope.disablePrev_group = true;
    $scope.disableNext_group = false;
    $scope.page_now_group = 1;
    //================================================================
	
	 //=====================paging events of group ======================
    $scope.entryLimit_eg = 5
    $scope.offset_eg = 0;
    $scope.disablePrev_eg = true;
    $scope.disableNext_eg = false;
    $scope.page_now_eg = 1;
    //================================================================
	
	 $scope.init = function() {
		     loginService.lock_admin(); 
			 $scope.link = loginService.getLink();
	    	 $scope.getAdminByAdminId($routeParams.id);
			 if($routeParams.id==0){
				$scope.text = "Create an admin";
				$scope.status=1;
				$scope.role=2;
			 }
		     else
				 $scope.text = "Update admin";
	    };
	
	 $scope.getAdminByAdminId = function(id){ 
	        $http.get($scope.link+'admin/'+id).success(function(data){
	        	console.log('an admin is loaded');
	        	//$scope.admin = data;
	        	$scope.firstname = data.data.firstname;
				$scope.lastname = data.data.lastname;
				$scope.username = data.data.username;
				$scope.password = data.data.password;
				$scope.status = data.data.status;
				$scope.role = data.data.role;
				$scope.id = data.data.id;
	        }).error(function(data, status, config) {
	        	console.log('can not load admin with admin id : ' + id);
	 });
	 }
	
	
	 $scope.submit = function() {
		     	if($routeParams.id==0)
				{
				  $scope.id = 0;
				}	
				
				adminService.submit_admin($scope.username,$scope.password,$scope.firstname,$scope.lastname,$scope.id,$scope.status,$scope.role);	
				
					//	 $http.get($scope.link+'admin/update/'+$scope.username+'/'+ $scope.password+'/'+ $scope.firstname+'/'+ $scope.lastname+'/'+ $scope.id+'/'+ $scope.status+'/'+ $scope.role ).success(function(data){
					//	console.log('update successful');
						
					//	if($routeParams.id==0)
					//	  alert('creat admin successful');
					 //   else 
					//	  alert('update successful');
					  
				//	}).error(function(data, status, config) {
					//	console.log('can not upload admin : ' + userObj);
				//		 alert('can not update or create admin');
				//	 });
					
			
	    };
	
	
	$scope.getUsersByGroupId = function(id){
		        $http.get($scope.link+'user_group/'+id).success(function(data){
		        	console.log('list users by group is loaded');
		        	$scope.lst_user = data;
		        	$scope.nb_users = data.data.members_list.length;
		        	$scope.jsonToArray_members(data);
		        	pageResult_group($scope);
		        }).error(function(data, status, config) {
		        	console.log('can not load list of users by group Id : ' + id);
		        });
		 }
	
		 $scope.jsonToArray_members = function (data) {
				$scope.b = new Array();
				var i = 0;
				angular.forEach(data.data.members_list,function(){
					$scope.b[i]=new Array(2);
		    		$scope.b[i][0] =  data.data.members_list[i].name;
					$scope.b[i][1] =  data.data.members_list[i].id;
		            i++;
		        })
		        $scope.nbr_rows_groups = i ;
				
				$scope.b_ini= angular.copy($scope.b);
			}
	
	
	 $scope.getUserById_group = function(id, type){
				var name = 'not_name';
			        $http.get($scope.link+'users/'+id).success(function(data){
			        	console.log('owner is loaded');
			            if(type==1){
			        	  $scope.owner = data;
			            }
			        	
			         
			        	// $scope.getGroupsByUserId(data.data.id);
			        }).success(function(data, status, config) {
			        	 $scope.name = data.data.first_name + ' ' + data.data.last_name;
			        });
			    
			    
			 }
	 

	$scope.getEventsByGroupId = function(id){
		        $http.get($scope.link+'group_event/'+id).success(function(data){
		        	console.log('list events by group is loaded');
		        	$scope.lst_events = data;
		        	$scope.nb_events = data.data.eventsList.length;
		        	$scope.jsonToArray_events(data);
		        	pageResult_group_event($scope);
		        }).error(function(data, status, config) {
		        	console.log('can not load list of events by group Id : ' + id);
		           });
	}
	
	
       $scope.jsonToArray_events = function (data) { //alert(JSON.stringify(data));
					$scope.d = new Array();
					var i = 0;
					angular.forEach(data.data.eventsList,function(){
						$scope.d[i]=new Array(2);
			    		
						$scope.d[i][1] =  data.data.eventsList[i].id;
						$scope.d[i][0] =  data.data.eventsList[i].name;
						
			            i++;
			        })
			        $scope.nbr_rows_eg = i ;
					$scope.d_ini= angular.copy($scope.d);
	 }	
	
	
     $scope.$watch('d', function() {   
			if( ($scope.page_now_eg==1)  &&  ($scope.nbr_rows_eg <= $scope.entryLimit_eg) )
			 {
			 $scope.disableNext_eg = true;
			 }
			
		}); 	
	
	 $scope.$watch('b', function() {   
			if( ($scope.page_now_group==1)  &&  ($scope.nbr_rows_groups <= $scope.entryLimit_group) )
			 {
			 $scope.disableNext_group = true;
			 }
			
		}); 
	 
		// switch to next page
		$scope.nextPage_group = function() { 
	       nextPage_common_group($scope);
		}

		// switch to previous page
		$scope.previousPage_group = function() {
	      previousPage_common_group($scope);
		}
	
	   // switch to next page
		$scope.nextPage_eg = function() { 
	       nextPage_evg($scope);
		}

		// switch to previous page
		$scope.previousPage_eg = function() {
	      previousPage_evg($scope);
		}
	
		
	
}])